<template>
  <el-dialog
    v-model="dialogVisible"
    :title="isEdit ? '编辑责任' : '新增责任'"
    width="800px"
    :before-close="handleClose"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="责任名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入责任名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="责任编号" prop="code">
            <el-input v-model="formData.code" placeholder="请输入责任编号" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="责任类型" prop="type">
            <el-select v-model="formData.type" placeholder="请选择责任类型" style="width: 100%">
              <el-option label="安全责任" value="safety" />
              <el-option label="运行责任" value="operation" />
              <el-option label="维护责任" value="maintenance" />
              <el-option label="管理责任" value="management" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="责任级别" prop="level">
            <el-select v-model="formData.level" placeholder="请选择责任级别" style="width: 100%">
              <el-option label="一级" value="1" />
              <el-option label="二级" value="2" />
              <el-option label="三级" value="3" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="归属部门" prop="department">
            <el-select v-model="formData.department" placeholder="请选择归属部门" style="width: 100%">
              <el-option label="管理部" value="管理部" />
              <el-option label="运行部" value="运行部" />
              <el-option label="维护部" value="维护部" />
              <el-option label="安全部" value="安全部" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-select v-model="formData.status" placeholder="请选择状态" style="width: 100%">
              <el-option label="有效" value="active" />
              <el-option label="暂停" value="suspended" />
              <el-option label="废止" value="abolished" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-form-item label="责任描述" prop="description">
        <el-input
          v-model="formData.description"
          type="textarea"
          :rows="4"
          placeholder="请输入责任描述"
        />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

interface Props {
  visible: boolean
  responsibilityData?: any
}

interface Emits {
  (e: 'update:visible', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  responsibilityData: null
})

const emit = defineEmits<Emits>()

const formRef = ref<FormInstance>()

const dialogVisible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value)
})

const isEdit = computed(() => !!props.responsibilityData?.id)

const formData = reactive({
  name: '',
  code: '',
  type: '',
  level: '',
  department: '',
  status: 'active',
  description: ''
})

const formRules: FormRules = {
  name: [
    { required: true, message: '请输入责任名称', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入责任编号', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择责任类型', trigger: 'change' }
  ],
  level: [
    { required: true, message: '请选择责任级别', trigger: 'change' }
  ],
  department: [
    { required: true, message: '请选择归属部门', trigger: 'change' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ],
  description: [
    { required: true, message: '请输入责任描述', trigger: 'blur' }
  ]
}

// 重置表单函数
const resetForm = () => {
  Object.assign(formData, {
    name: '',
    code: '',
    type: '',
    level: '',
    department: '',
    status: 'active',
    description: ''
  })
  formRef.value?.clearValidate()
}

// 监听数据变化，初始化表单
watch(() => props.responsibilityData, (newData) => {
  if (newData) {
    Object.assign(formData, {
      name: newData.name || '',
      code: newData.code || '',
      type: newData.type || '',
      level: newData.level || '',
      department: newData.department || '',
      status: newData.status || 'active',
      description: newData.description || ''
    })
  } else {
    resetForm()
  }
}, { immediate: true })

const handleClose = () => {
  dialogVisible.value = false
  resetForm()
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    
    // 这里应该调用API保存数据
    console.log('保存责任数据:', formData)
    
    ElMessage.success(isEdit.value ? '编辑成功' : '新增成功')
    emit('success')
    handleClose()
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>